<?php

namespace common\logics;

use Yii;
use common\models\ofc\OfcSeason;

/**
 * 赛程基础逻辑模型
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class ScheduleLogic extends \common\models\ofc\OfcMatch implements \common\db\SearchModelInterface {

    /**
     * @var integer|false 请求方向，0向下加载，1追溯加载 
     */
    public $before = 0;

    /**
     * @var boolean 是否使用请求方向 
     */
    public $useBeforeDirection = true;

    /**
     * @var integer 球队ID
     */
    public $teamid;

    /**
     * @var boolean 当前比赛 
     */
    public static $current = false;

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['teamid'], 'required'],
            [['teamid', 'season_id'], 'integer'],
            [['before'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function search() {
        if (!$this->validate()) {
            return $this->dumpError();
        }
        return parent::search();
    }

    /**
     * 构建ActiveRecord查询
     * @inheritdoc
     */
    public function buildQuery() {
        $query = static::find()->active();
        $checkAll = false;
        if ($this->season_id) {
            $query->andFilterWhere(['season_id' => $this->season_id]);
            $season = OfcSeason::findOne($this->season_id);
            if ($season->competition->type != 1) {
                $checkAll = true;
            }
        } else {
            $query->innerJoinWith('season s')->andWhere(['s.current' => 1]);
        }
        $query->andWhere([
            'or',
            ['host_team_id' => $this->teamid],
            ['guest_team_id' => $this->teamid],
        ]);
        if ($this->useBeforeDirection !== false) {
            if (!$checkAll) {
                if ($this->before) {
                    $query->andWhere(['match_status' => self::MATCH_STATUS_COMPLETED]);
                    $query->orderBy(['match_time' => SORT_DESC]);
                } else {
                    $query->andWhere(['<', 'match_status', self::MATCH_STATUS_COMPLETED]);
                    $query->orderBy(['match_time' => SORT_ASC]);
                }
            } else {
                if ($this->before) {
                    $query->andWhere(['id' => 0]);
                }
                $query->orderBy(['match_time' => SORT_DESC]);
            }
        } else {
            $query->orderBy(['match_time' => SORT_ASC]);
        }
        return $query;
    }

    public function getCurrent() {
        if (self::$current) {
            return false;
        }
        $currenttime = time(); //strtotime('2019-03-04 12:12:12');
        $durtion = Yii::$app->formatter->asDateDiff($this->match_time, $currenttime);
        if ($this->match_time <= $currenttime) {
            $durtion *= -1;
        }
        if ($durtion >= 0) {
            return self::$current = true;
        }
        return false;
    }

    public function getHostTeam() {
        return $this->hasOne(TeamLogic::className(), ['id' => 'host_team_id']);
    }

    public function getGuestTeam() {
        return $this->hasOne(TeamLogic::className(), ['id' => 'guest_team_id']);
    }

    /**
     * @inheritdoc
     */
    public function fields() {
        return [
            'id',
            'title' => 'name',
            'host_team' => 'hostTeam',
            'guest_team' => 'guestTeam',
            'host_score',
            'guest_score',
            'court',
            'match_time',
            'match_status' => 'matchStatus',
        ];
    }

}
